/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-26
*/
package com.rzico.basics.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.MemberFavorites;
import com.rzico.basics.entity.Product;
import com.rzico.basics.model.ProductVo;

import com.rzico.basics.service.MemberFavoritesService;
import com.rzico.basics.service.MemberService;
import com.rzico.basics.service.ProductService;

import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收藏控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-26
 */
@Api(description = "收藏接口")
@RestController
@RequestMapping("/member/memberFavorites")
public class MemberFavoritesMemberController extends BaseController {

    @Autowired
    private MemberFavoritesService memberFavoritesService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ProductService productService;

    /**
     * 分页查询收藏商品
     *
     * @return
    */
    @ApiOperation("分页查询收藏商品")
    @GetMapping("/list")
    public CommResult<ProductVo> list(Long memberId,Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);


        Member member = memberService.getCurrent();

        if (member!=null) {
            params.put("memberId",member.getId());
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ProductVo> list = memberFavoritesService.selectProductVoList(params);
        PageResult<ProductVo> pageResult = new PageResult<ProductVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 获取收藏商品数量
     *
     * @return
     */
    @ApiOperation("获取收藏商品数量")
    @GetMapping("/count")
    public CommResult<Long> count() {
        Member member = memberService.getCurrent();
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",member.getId());
        //获取收藏
        Integer fav = memberFavoritesService.selectRowCount(params);
        return CommResult.success(fav);

    }


    /**
     * 检是是否有收藏
     *
     * @return
     */
    @ApiOperation("检是是否有收藏")
    @GetMapping("/exists")
    public CommResult<Long> exists(Long productId) {
        Member member = memberService.getCurrent();
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",member.getId());
        params.put("productId",productId);
        //获取收藏
        Integer fav = memberFavoritesService.selectRowCount(params);
        return CommResult.success(fav);

    }

    /**
     * 添加收藏
     *
     * @param memberFavorites
     * @return
     */
    @ApiOperation("添加收藏")
    @PostMapping("/add")
    public CommResult<MemberFavorites> add(@RequestBody MemberFavorites memberFavorites) {
        Member member = memberService.getCurrent();
        Product product = productService.findById(memberFavorites.getProductId());
        memberFavorites.setEnterpriseId(product.getEnterpriseId());
        memberFavorites.setCreateDate(new Date());
        memberFavorites.setModifyDate(new Date());
        memberFavorites.setMemberId(member.getId());
        memberFavorites.setProductType(0);
        int affectCount = memberFavoritesService.insertUseGeneratedKeys(memberFavorites);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        MemberFavorites result = memberFavoritesService.findById(memberFavorites.getId());
        return CommResult.success("收藏成功");

    }


    /**
     * 批量删除收藏
     *
     * @return
     */
    @Log(desc = "批量删除收藏", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除收藏,ids用逗号拼接")
    @PostMapping("/del")
    @ApiImplicitParams({@ApiImplicitParam(name = "productId", value = "商品ID", dataType = "String", paramType = "query")
    })
    public CommResult<MemberFavorites> del(Long productId) {

        Member member = memberService.getCurrent();

        MemberFavorites memberFavorites = new MemberFavorites();
        memberFavorites.setProductId(productId);
        memberFavorites.setMemberId(member.getId());

        memberFavoritesService.delete(memberFavorites);
        return CommResult.success("取消收藏");
    }

}
